/*
 * Copyright (c) 2010-2011 NOO. All Rights Reserved.
 * [Id:DefModuleCmp.java  2011-11-05 下午3:38 poplar.yfyang ]
 */
package org.mumu.pawn.cmps.def;

import org.joda.time.DateTimeUtils;
import org.mumu.pawn.cmps.ModuleCmp;
import org.mumu.pawn.domain.Module;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * <p>
 * 模块管理实现.
 * </p>
 *
 * @author poplar.yfyang
 * @version 1.0 2011-11-05 下午3:38
 * @since JDK 1.5
 */
public class DefModuleCmp implements ModuleCmp {
    private static final long serialVersionUID = 7358380035622128580L;

    @Override
    public List<Module> getModuleList() {
        Query query = em.createQuery("from Module m order by m.sort desc");
        return query.getResultList();
    }

    @Override
    public void saveOrUpdateModule(Module module) {
        if (module.getId() == 0) {
            module.setCreateActor(1);
            module.setCreateTime(DateTimeUtils.currentTimeMillis());
            em.persist(module);
        } else {
            module.setUpdateActor(1);
            module.setUpdateTime(DateTimeUtils.currentTimeMillis());
            em.merge(module);
        }
    }

    @Override
    public void removeModule(long id) {
        Module m = findModule(id);
        em.remove(m);
    }

    @Override
    public Module findModule(long id) {

        return em.find(Module.class, id);
    }

    @Inject
    private EntityManager em;

}
